﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using DAL.BAL;
using DevExpress.Xpf.Editors.Settings;
using DevExpress.Xpf.Grid;

namespace WpfClient.Windows
{
    /// <summary>
    /// Interaction logic for Users.xaml
    /// </summary>
    public partial class Users : Window
    {
        public Users()
        {
            InitializeComponent();

            if (!Business.CanEditUsers())
            {
                gridControl1.View.AllowEditing = false;

                deleteRowItem.IsVisible = false;

                bDelete.IsVisible = false;

                bSave.IsVisible = false;

                bCancel.IsVisible = false;
            }
            
            Refresh();
        }

        private void Refresh()
        {
            try
            {
                gridControl1.View.CancelRowEdit();

                Business.Refresh(System.Data.Objects.RefreshMode.ClientWins, Business.MyEntities.Users);

                ((ComboBoxEditSettings)gridControl1.Columns["Role"].EditSettings).ItemsSource = Business.GetRoles();

                gridControl1.ItemsSource = Business.GetUsers();
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message, WpfClient.Properties.Resources.Error, MessageBoxButton.OK);
            }
        }

        private void deleteRowItem1_ItemClick(object sender, DevExpress.Xpf.Bars.ItemClickEventArgs e)
        {
            if (MessageBox.Show(WpfClient.Properties.Resources.AreYouSureYouWantDelete,
                WpfClient.Properties.Resources.Warning, MessageBoxButton.YesNoCancel, MessageBoxImage.Warning) == MessageBoxResult.Yes)
            {
                gridControl1.View.CommitEditing();

                try
                {
                    gridControl1.View.FocusedRowChanged -= tableView_FocusedRowChanged;

                    (gridControl1.View as GridViewBase).DeleteRow(gridControl1.View.FocusedRowHandle);

                    Business.Save();
                }
                finally
                {
                    gridControl1.View.FocusedRowChanged += tableView_FocusedRowChanged;
                }
            }
        }

        private void gridControl1_Unloaded(object sender, RoutedEventArgs e)
        {
            Save();
        }

        private void RefreshItem_ItemClick(object sender, DevExpress.Xpf.Bars.ItemClickEventArgs e)
        {
            Refresh();
        }
        
        private void TableView_ValidateRow(object sender, GridRowValidationEventArgs e)
        {
            if (((DAL.Models.User)e.Row).UserName == null)
            {
                e.ErrorContent = WpfClient.Properties.Resources.Field_Cant_Be_Empty + " (" +
                                 gridControl1.Columns["UserName"].Header + ")";
                e.ErrorType = DevExpress.XtraEditors.DXErrorProvider.ErrorType.Warning;
                e.IsValid = false;

            }
            else
                if (((DAL.Models.User)e.Row).Password == null)
                {
                    e.ErrorContent = WpfClient.Properties.Resources.Field_Cant_Be_Empty + " (" +
                                     gridControl1.Columns["Password"].Header + ")";
                    e.ErrorType = DevExpress.XtraEditors.DXErrorProvider.ErrorType.Warning;
                    e.IsValid = false;

                }
                else
                    if (((DAL.Models.User)e.Row).Role == null)
                    {
                        e.ErrorContent = WpfClient.Properties.Resources.Field_Cant_Be_Empty + " (" +
                                         gridControl1.Columns["Role"].Header + ")";
                        e.ErrorType = DevExpress.XtraEditors.DXErrorProvider.ErrorType.Warning;
                        e.IsValid = false;

                    }
                    else
                        e.IsValid = true;
            
            
        }

        private void TableView_InvalidRowException(object sender, InvalidRowExceptionEventArgs e)
        {
            e.ExceptionMode = ExceptionMode.NoAction;
        }

        private void tableView_FocusedRowChanged(object sender, FocusedRowChangedEventArgs e)
        {
            try
            {
                gridControl1.View.FocusedRowChanged -= tableView_FocusedRowChanged;

                Save();
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK);
            }
            finally
            {
                gridControl1.View.FocusedRowChanged += tableView_FocusedRowChanged;
            }
        }

        private void Save()
        {
            try
            {
                gridControl1.View.CommitEditing();

                Business.Save();
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message, WpfClient.Properties.Resources.Error, MessageBoxButton.OK);
            }
        }

        private void bSave_ItemClick(object sender, DevExpress.Xpf.Bars.ItemClickEventArgs e)
        {
            Save();
        }

        private void bCancel_ItemClick(object sender, DevExpress.Xpf.Bars.ItemClickEventArgs e)
        {
            gridControl1.View.CancelRowEdit();

            Business.Cancel();
        }
        
    }
}
